function changeSate(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/templatemsg/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {
        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/templatemsg/list'});
                } else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

var editsw = [];
/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var id = button.data('row');
    console.log(button.data('row'))
    var modal = $(this);
    $.ajax({
        type: 'POST',
        url: "getTemplatemsgById",
        data: {
            id: id
        },
        success: function (data) {
            console.log(data)
            modal.find('#id').val(data.id)
            modal.find('#tplid').val(data.tplid)
            modal.find('#tplcode').val(data.tplcode)
            modal.find('#tplname').val(data.tplname)
            modal.find('#page').val(data.page)
            modal.find('#keyname1').val(data.keyname1)
            modal.find('#keyname2').val(data.keyname2)
            modal.find('#keyname3').val(data.keyname3)
            modal.find('#keyname4').val(data.keyname4)
            modal.find('#keyname5').val(data.keyname5)
            modal.find('#keycode1').val(data.keycode1)
            modal.find('#keycode2').val(data.keycode2)
            modal.find('#keycode3').val(data.keycode3)
            modal.find('#keycode4').val(data.keycode4)
            modal.find('#keycode5').val(data.keycode5)
            editsw["isonce"] == undefined ? editsw["isonce"] = new Switchery(document.querySelector('#isonce'), {color: "#1AB394"}) : ""
            if (data.isonce == 1) {
                setSwitchery(editsw["isonce"], true);
            } else {
                setSwitchery(editsw["isonce"], false);
            }

            if (data.state == 1) {
                modal.find('#state').attr("checked", 'checked').val("1");
            }
        },
        dataType: "json"
    });
})

//tableHeight函数
function tableHeight() {
    //可以根据自己页面情况进行调整
    return $(window).height() - 200;
}

$(function () {
    var sw = [];
    $(".js-switch").each(function (i, e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })

    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })

    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url: "/admin/templatemsg/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination: true,//是否分页
        queryParamsType: 'limit',
        queryParams: queryParams,
        sidePagination: 'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize: 50,//单页记录数
        pageList: [50, 100, 200, 500],//分页步进值
        showExport: true,//显示导出
        showRefresh: false,//刷新按钮
        showColumns: false,
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns: [
            {
                title: '全选',
                field: 'select',
                checkbox: true,
                width: 25,
                align: 'center',
                valign: 'middle'
            },
            {
                title: 'ID',
                field: 'id',
                visible: false
            },
            {
                title: '模板ID',
                field: 'tplid'
            },
            {
                title: '模板code',
                field: 'tplcode'
            },
            {
                title: '标题',
                field: 'tplname'
            },
            {
                title: '跳转页面',
                field: 'page'
            },
            {
                title: '内容1',
                field: 'keyname1'
            },
            {
                title: '内容2',
                field: 'keyname2'
            },
            {
                title: '内容3',
                field: 'keyname3'
            },
            {
                title: '内容4',
                field: 'keyname4'
            },
            {
                title: '内容5',
                field: 'keyname5'
            },
            {
                title: '字段1',
                field: 'keycode1'
            },
            {
                title: '字段2',
                field: 'keycode2'
            },
            {
                title: '字段3',
                field: 'keycode3'
            },
            {
                title: '字段4',
                field: 'keycode4'
            },
            {
                title: '字段5',
                field: 'keycode5'
            },
            {
                title: '是否一次性订阅',
                field: 'isonce',
                formatter: sfFormatter
            },
            {
                title: '操作',
                field: 'Attribute',
                align: 'center',
                formatter: actionFormatter
            }
        ],
        locale: 'zh-CN',//中文支持,
    })

    //请求服务数据时所传参数
    function queryParams(params) {
        var p = {
            limit: params.limit,
            offset: params.offset,
            sort: params.sort,
            order: params.order,
            search: $('#search').val(),
            status: $('#qstatus').val()
        }
        return p;
    }

    $('#search_btn').click(function () {
        var params = {
            status: $('#qstatus').val(),
            search: $('#search').val(),
            ts: new Date().getTime()
        };
        console.log(params);
        $('#mytable').bootstrapTable('refresh', params);
    })




})

function checkAdd() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name") + "=" + $(this).val())
        }
    });

    // if( $("#type").val()==''){
    //     swal({title: "信息提示", text: "请选择优惠券类型", type: "error"})
    //     return false
    // }
    // $('input').each(function (index, item) {
    //     if ($(this).attr("name")=="total"&&$(this).val()*1<=0){
    //         swal({title: '库存不能为空且必须大于0', text: '', timer: 2000})
    //         return false
    //     }
    // });

    return true;
}

function checkModify() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id") + "=" + $(this).val())
        }
    });

    // if( $("#type").val()==''){
    //     swal({title: "信息提示", text: "请选择优惠券类型", type: "error"})
    //     return false
    // }
    // if ($("#total").val()*1<=0){
    //     swal({title: '库存不能为空且必须大于0', text: '', timer: 2000})
    //     return false
    // }
    return true;
}